<template>
  <div class="sh_wrap">
    <div class="sh_frame_box">
      <!-- <van-nav-bar title="掌上搜索" /> -->
      <!-- 搜索框组件 -->
      <div class="boxseach">
        <input
          type="seach"
          class="seach"
          placeholder="搜索达人/商品/直播"
          v-model="search"
          @blur="searchImg=false"
          @focus="searchImg=true"
        />
        <!-- 放大镜图片（定位到固定位置） -->
        <div class="search_img_box">
          <img
            v-if="!searchImg"
            src="@/assets/two/seach.png"
            class="imgseach"
            @click="searchEvent"
          />
          <img
            v-if="searchImg"
            src="@/assets/one/search.png"
            class="imgseach"
            @click="searchEvent"
          />
        </div>
      </div>
    </div>
    <div class="sh_record_box" v-if="showPage">
      <div class="shr_tlt_box">
        <div class="shr_tlt">历史搜索</div>
        <div class="shr_delete" @click="deleteSearchLog">
          <img src="@/assets/one/delete.png" alt="" />
        </div>
      </div>
      <!-- 每一项搜索内容 -->
      <div class="sh_record_item">
        <div
          class="shr_box"
          v-for="(item, index) in searchLog"
          :key="index"
          @click="itemSearch(item.log)"
        >
          {{ item.log }}
        </div>
      </div>
      <div class="no_data" v-if="searchLog == '' || searchLog.length == 0">
        <img src="@/assets/one/zw.png" alt="" />
        <div class="zw">暂无搜索记录~</div>
      </div>
    </div>

    <div class="sh_tabs" v-if="!showPage">
      <van-tabs v-model="active">
        <van-tab title="达人">
          <template v-if="!listLoading">
            <div class="sh_talent" v-for="(item, index) in talentList" :key="index" @click="toTalentPage(item)">
              <div class="sh_talent_i">
                <img :src="item.logo" alt="" />
              </div>
              <div class="sh_talent_r">
                <div class="t">{{ item.nickname }}</div>
                <div class="b">粉丝数: {{ format_num(item.total_fans) }}</div>
              </div>
            </div>
          </template>
          <!-- ======== 达人缺省图 ============= -->
          <div class="no_data" v-if="talentList.length == 0 && !listLoading">
            <img src="@/assets/one/zw.png" alt="" />
            <div class="zw">暂无数据</div>
          </div>
          <!-- ======== loading ============ -->
          <van-loading
            style="margin: 2.5rem 0rem"
            class="loading"
            v-if="listLoading"
            type="spinner"
            :vertical="true"
          />
        </van-tab>
        <van-tab title="商品">
          <div class="sh_goods" v-for="(item, index) in goodsList" :key="index" @click="toGoodsPage(item)">
            <div class="sh_goods_i">
              <img :src="item.logo" alt="" />
            </div>
            <div class="sh_goods_r">
              <div class="t">{{ item.title }}</div>
              <div class="b"><span>￥</span>{{ item.price }}</div>
            </div>
          </div>
          <!-- ======== 商品缺省图 ============= -->
          <div class="no_data" v-if="goodsList.length == 0 && !listLoading">
            <img src="@/assets/one/zw.png" alt="" />
            <div class="zw">暂无数据</div>
          </div>
          <!-- ======== loading ============ -->
          <van-loading
            style="margin: 2.5rem 0rem"
            class="loading"
            v-if="listLoading"
            type="spinner"
            :vertical="true"
          />
        </van-tab>
        <van-tab title="直播">
          <div class="sh_live" v-for="(item, index) in liveList" :key="index" @click="toLivePage(item)">
            <div class="sh_live_i">
              <img :src="item.logo" alt="" />
            </div>
            <div class="sh_live_r">
              <div class="t">{{ item.room_title }}</div>
              <div class="b">
                开播时间：{{ timestamp(item.begin_timestamp, "Y-M-D") }}
              </div>
            </div>
          </div>
          <!-- ======== 直播缺省图 ============= -->
          <div class="no_data" v-if="liveList.length == 0 && !listLoading">
            <img src="@/assets/one/zw.png" alt="" />
            <div class="zw">暂无数据</div>
          </div>
          <!-- ======== loading ============ -->
          <van-loading
            style="margin: 2.5rem 0rem"
            class="loading"
            v-if="listLoading"
            type="spinner"
            :vertical="true"
          />
        </van-tab>
      </van-tabs>
    </div>
  </div>
</template>

<script>
export default {
  components: {},
  data() {
    return {
      active: 0,
      showPage: true,
      search: "",
      listLoading: false, //列表搜索loading
      searchLog: [], //搜索记录

      talentList: [], //达人列表
      goodsList: [], //商品列表
      liveList: [], //直播列表
      searchImg: false,
    };
  },
  props: {},
  watch: {
    search(newVal) {
      if (!newVal) {
        this.showPage = true;
        this.getSearchLog();
      }
    },
  },
  computed: {},
  created() {
    this.getSearchLog();
    this.routeData()
  },
  mounted() {},
  methods: {
    // 搜索 达人,商品,直播
    searchEvent() {
      if (this.search) {
        this.talentList = [];
        this.goodsList = [];
        this.liveList = [];
        this.listLoading = true;
        this.showPage = false;
        this.$axios
          .get("/api/search", {
            params: {
              keyword: this.search,
            },
          })
          .then((res) => {
            this.listLoading = false;
            this.talentList = res.data.data.author;
            this.goodsList = res.data.data.product;
            this.liveList = res.data.data.live;
          });
      }
      this.$axios
        .get("/api/customer/RecordSearchLog", {
          params: {
            key: this.search,
          },
        })
        .then((res) => {
          console.log(res);
        });
      if (!this.search) {
        this.showPage = true;
      }
    },

    // 获取历史搜索记录
    getSearchLog() {
      this.$axios.get("/api/customer/GetSearchLog").then((res) => {
        this.searchLog = res.data.data;
      });
    },

    // 删除记录
    // /api/customer/DeleteSearchLog?ids=297
    deleteSearchLog() {
      let itemLod = "";
      this.searchLog.forEach((item) => {
        itemLod += item.id + ",";
      });
      itemLod = itemLod.substring(0, itemLod.lastIndexOf(","));
      this.$axios
        .get("/api/customer/DeleteSearchLog", {
          params: {
            ids: itemLod,
          },
        })
        .then((res) => {
          this.getSearchLog();
        });
    },

    // 历史记录每一项的点击搜索
    itemSearch(name) {
      this.search = name;
      this.searchEvent();
    },

    // 跳转达人详情
    toTalentPage(item){
        this.$router.push({
            path: 'talentDetailsMain',
            query:{
                id: item.author_id
            }
        })
    },
    // 跳转商品详情
    toGoodsPage(item){
        this.$router.push({
          path:'/goodsDetailsMain',
          query: {
            id: item.pro_id
          }
        })
    },
    // 跳转直播详情
    toLivePage(item){
        this.$router.push({
          path:'/LiveForDetailsMain',
          query: {
            room_id: item.room_id,
            author_id: item.author_id
          }
        })
    },
    
    routeData(){
      if(this.$route.query.name){
        this.active = Number(this.$route.query.value)
        this.search = this.$route.query.name
        this.searchEvent();
      }
    }
  },
};
</script>

<style scoped lang="less">
.sh_wrap {
  min-height: 100vh;
  background-color: #f1f1f1;
  display: flex;
  flex-direction: column;
}
.sh_frame_box {
  padding: 0 0.4rem;
  background-color: #fff;
  padding-top: 10px;
  .boxseach {
    background: #fff;
    width: 100%;
    background: #f1f1f1;
    display: flex;
    border-radius: 0.453333rem;
    .seach {
      width: 85%;
      height: 0.906667rem;
      border-radius: 0.453333rem;
      border: none;
      background: #f1f1f1;
      font-size: 0.33rem;
      color: #222;
      padding-left: 0.426667rem;
      box-sizing: border-box;
    }
    .search_img_box {
      width: 15%;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .imgseach {
      width: 0.4rem;
      height: 0.4rem;
    }
  }
}
.sh_record_box {
  padding: 0 0.4rem;
  padding-top: 0.68rem;
  background-color: #fff;
  flex: 1;
  .shr_tlt_box {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .shr_tlt {
      font-size: 0.4rem;
      color: #222222;
      font-weight: 600;
    }
    .shr_delete {
      width: 1.333333rem;
      text-align: right;
    }
    img {
      width: 0.426667rem;
      height: 0.426667rem;
    }
  }
  .no_data {
    width: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    margin-top: 2.666667rem;
    img {
      width: 4rem;
      height: 2.8rem;
    }
    .zw {
      font-size: 0.346667rem;
      color: #888888;
    }
  }
}
.sh_record_item {
  display: flex;
  flex-wrap: wrap;
  .shr_box {
    padding: 0.226667rem 0.4rem;
    font-size: 0.346667rem;
    color: #555555;
    background-color: #f7f7f7;
    border-radius: 0.133333rem;
    margin-right: 0.4rem;
    margin-top: 0.4rem;
    word-break: break-all;
  }
}

.sh_tabs {
  flex: 1;
  /deep/.van-tabs {
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }
  /deep/.van-tabs__line{
    width: 0.533333rem;
    background-color: #fd7f2c;
  }
  /deep/.van-tabs__content {
    padding: 0 0.4rem;
    box-sizing: border-box;
    background-color: #f1f1f1;
    padding-bottom: 0.8rem;
  }
  .sh_talent {
    width: 100%;
    height: 1.72rem;
    margin-top: 0.266667rem;
    background-color: #fff;
    border-radius: 0.133333rem;
    display: flex;
    align-items: center;
    padding-left: 0.266667rem;
    box-sizing: border-box;
    .sh_talent_i {
      width: 1.173333rem;
      height: 1.173333rem;
      border-radius: 50%;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }
    .sh_talent_r {
      margin-left: 0.266667rem;
      .t {
        width: 6.666667rem;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        font-size: 0.4rem;
        color: #222222;
      }
      .b {
        margin-top: 0.053333rem;
        font-size: 0.32rem;
        color: #888888;
      }
    }
  }
  .sh_goods {
    width: 100%;
    height: 1.72rem;
    margin-top: 0.266667rem;
    background-color: #fff;
    border-radius: 0.133333rem;
    display: flex;
    align-items: center;
    padding-left: 0.266667rem;
    box-sizing: border-box;
    .sh_goods_i {
      width: 1.173333rem;
      height: 1.173333rem;
      border-radius: 0.266667rem;
      img {
        width: 100%;
        height: 100%;
        border-radius: 0.266667rem;
      }
    }
    .sh_goods_r {
      margin-left: 0.266667rem;
      .t {
        width: 6.666667rem;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        font-size: 0.4rem;
        color: #222222;
      }
      .b {
        margin-top: 0.053333rem;
        font-size: 0.426667rem;
        color: #fd7f2c;
        font-family: Dinpro-medium;
        span {
          font-size: 0.346667rem;
        }
      }
    }
  }
  .sh_live {
    width: 100%;
    height: 1.72rem;
    margin-top: 0.266667rem;
    background-color: #fff;
    border-radius: 0.133333rem;
    display: flex;
    align-items: center;
    padding-left: 0.266667rem;
    box-sizing: border-box;
    .sh_live_i {
      width: 1.173333rem;
      height: 1.173333rem;
      border-radius: 0.266667rem;
      img {
        width: 100%;
        height: 100%;
        border-radius: 0.266667rem;
      }
    }
    .sh_live_r {
      margin-left: 0.266667rem;
      .t {
        width: 6.666667rem;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        font-size: 0.4rem;
        color: #222222;
      }
      .b {
        margin-top: 0.053333rem;
        font-size: 0.32rem;
        color: #888888;
      }
    }
  }
  .no_data {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    margin-top: 2.666667rem;
    img {
      width: 4rem;
      height: 2.8rem;
    }
    .zw {
      font-size: 0.346667rem;
      color: #888888;
    }
  }
  .tld_list_nodata {
    width: 100%;
    height: 6rem;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    img {
      width: 4rem;
      height: 2.8rem;
    }
    .zw {
      font-size: 0.346667rem;
      color: #888888;
    }
  }
}
</style>
